package com.controller;
import com.entity.Subject;
import com.entity.UserInfo;
import com.entity.vo.SubjectVo;
import com.service.OptionService;
import com.service.SubjectService;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
@Controller
public class OptionController {
    @Resource
    private SubjectService subjectService;
    @Resource
    private OptionService optionService;
    @RequestMapping("/vote")
    public String vote(Integer subid,Model model) {
    	//查询subject详细
    	SubjectVo svo =  this.subjectService.selectOneSubjectDetail(subid);
		//根据subid查询详细的option(选项)
    	List<Map> lists =  this.optionService.selectOptionDetail(subid);
    	model.addAttribute("onevo", svo );
    	model.addAttribute("op", lists );
    	return "vote";
    }
    @RequestMapping("/deleteOptionByid")
   public String deleteOptionByid(Integer opid,Integer vsid , HttpSession session) {
    	Integer temp = this.optionService.deleteOption(opid);
    	if(temp == 2) {
    		session.setAttribute("deloperr", "1");
    	}else {
    		session.setAttribute("delopok", "1");
    	}
	   return "redirect:/selectSubjectAndOptionInfo?vsid="+vsid;
   }
    // ids option��id  ops option������  sub ������Ϣ
    @RequestMapping("/adminEditOption")
    public String adminEditOption(Integer[] ids, String[] ops, Subject sub, HttpSession session) {
    	System.out.println(sub.getVs_id() + " \t " + sub.getVs_title() + " \t " + sub.getVs_type());
    	for(int i = 0 ; i < ops.length; i++) {
    		System.out.print(ops[i] + ", ");
    	}
    	System.out.println();
    	if(ids != null && ids.length > 0 ) {
    	for(int i = 0 ; i < ids.length; i++) {
    		System.out.print(ids[i] + ", ");
    	 }
    	}
    	Integer t = this.optionService.editOption(ids, ops, sub);
    	if(t > 0 ) {
    		//保存状态
    		session.setAttribute("editok", "1");
    	}
    	return "redirect:/adminselectsubjectDetail";
    }
}
